package cn.gobang.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

/**
 * 把对象作为JSON格式响应
 * @author 吴新国 
 *
 */
public class ResultUtils {
	/**
	 * @param response HtttpServletResponse对象
	 * @param data 需要转化为JSON数据的对象
	 * @throws IOException 如果发生流错误，抛出该异常
	 */
    public static void toJson(HttpServletResponse response, Object data) 
            throws IOException {
            String result =JSON.toJSONString(data);
            response.setContentType("text/json; charset=utf-8");
            response.setHeader("Cache-Control", "no-cache"); //清除浏览器缓存
            PrintWriter out = response.getWriter();
            out.print(result);
            out.flush();
            out.close();
        }
}
